package com.mall.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.dao.mapper.AdminUserMapper;
import com.mall.model.AdminUser;
import com.mall.model.AdminUserExample;
import com.mall.model.AdminUserExample.Criteria;

@Service
public class AdminUserService {
	@Autowired
	private AdminUserMapper adminUsermapper;

	public boolean update(String password, String oldPwd, Integer id) {
		AdminUser selectByPrimaryKey = adminUsermapper.selectByPrimaryKey(id);
		if (selectByPrimaryKey != null) {
			String old = selectByPrimaryKey.getPassword();
			if (!old.equals(oldPwd)) {
				return false;
			}
			selectByPrimaryKey.setPassword(password);
			adminUsermapper.updateByPrimaryKey(selectByPrimaryKey);
			return true;
		}
		return false;
	}

	public AdminUserMapper getAdminUsermapper() {
		return adminUsermapper;
	}

	public void setAdminUsermapper(AdminUserMapper adminUsermapper) {
		this.adminUsermapper = adminUsermapper;
	}

	/**
	 * 登录验证
	 *
	 * @dagewang 2017年9月25日
	 */
	public AdminUser getByName(String username, String password) {
		AdminUserExample ex = new AdminUserExample();
		Criteria createCriteria = ex.createCriteria();
				createCriteria.andUsernameEqualTo(username);
				createCriteria.andPasswordEqualTo(password);
			List<AdminUser> selectByExample = adminUsermapper.selectByExample(ex);
						if(selectByExample.size()!=0){
						return selectByExample.get(0);	
						}
						return null;
	}

}
